#include "msp430x41x.h"

extern unsigned char signal_flag;
extern unsigned char reset_flag;
extern void delays(int x);
extern long count;
extern long threshold;
extern void Display(long count);

unsigned char Judge_Signal(void)
{
    unsigned char tmp;
    tmp=P6IN&BIT3;
    if(tmp==0)
    {
        signal_flag=0;
        return 0;
    }
    
    if((tmp==BIT3)&&(signal_flag==0))
    {
        delays(5);
        tmp=P6IN&BIT3;
        if(tmp==BIT3)
        {
            signal_flag=1;
            return 1;
        }
    }
    return 0;
}

unsigned char Judge_Reset(void)
{
    unsigned char tmp;
    tmp=P6IN&BIT4;
    if(tmp==BIT4)
    {
        reset_flag=0;
        return 0;
    }
    
    if((tmp==0)&&(reset_flag==0))
    {
        delays(150);
        tmp=P6IN&BIT4;
        if(tmp==0)
        {
            reset_flag=1;
            return 1;
        }
    }
    return 0;
}

void Switch_Output(void)
{
    P6OUT ^= BIT5;                            // Toggle P6.5
}

void AddOne(void)
{
    count++;
    if(count>=threshold)//threshold
    {
    count=threshold; //threshold

        if(signal_flag==0)
        {
        signal_flag=1;
        Switch_Output();    
        }

    }
    Display(count);

}
